package com.rututor.server.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailServices {
	private static final Logger Log  = Logger.getLogger(EmailServices.class.getName()); //Need to set properties to use logging
	public static String newline = System.getProperty("line.separator");
	//TODO: This is fast and dirty e-mail function needs to be more generic. 
	public static void sendNewApplicationEmail(String name, String blobString) {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		try {
			String msgBody = name + " has created a new application for RU Tutor";
			msgBody = msgBody + newline+newline;
			msgBody = msgBody + "http://rututor.me/resume/"+URLEncoder.encode(name,"UTF-8")+"?blob-key="+blobString;
			msgBody = msgBody + newline;
			
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("ajayxmehta@gmail.com","RU Tutor System"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"vinay@rututor.me", "RU Tutor System"));
			msg.setSubject("RU Tutor System : New RU Tutor Application Created");
			msg.setText(msgBody);
			
			Transport.send(msg);

		} catch (AddressException e) {
			Log.severe(e.getMessage());
			e.printStackTrace();
		} catch (MessagingException e) {
			Log.severe(e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			Log.severe(e.getMessage());
			e.printStackTrace();
		}

	}
}